home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-01-28 | 12.2 KB | 510 lines | [TEXT/MPS ] |
- /*
- File: ObjModelEvents.cp
-
- Contains: Apple Event handlers implementation
-
- Developed by:
-
- Paul G Smith (commstalk hq & Full Moon Software, Inc)
-
- you can leave messages at (UK): 0727 844232; (US): 408 253 7199
- BUT I prefer to be contacted by e-mail
- AppleLink: SMITH.PG
- Internet: SMITH.PG@applelink.apple.com
-
- "SimpliFace" Sample code to accompany develop article
- on techniques for embedding scripts in applications.
-
-
- Apple Event handlers for SimpliFace
-
- */
-
- #ifndef __STDIO__
- #include <StdIO.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
- #ifndef __WINDOWS__
- #include <Windows.h>
- #endif
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
- #ifndef __QUICKDRAW__
- #include <Quickdraw.h>
- #endif
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
- #ifndef __FILES__
- #include <Files.h>
- #endif
- #ifndef __STANDARDFILE__
- #include <StandardFile.h>
- #endif
- #ifndef __SYSEQU__
- #include <SysEqu.h>
- #endif
- #ifndef __PLSTRINGFUNCS__
- #include <PLStringFuncs.h>
- #endif
-
- #ifndef __AERegistry__
- #include <AERegistry.h>
- #endif
- #ifndef __ASREGISTRY__
- #include <ASRegistry.h>
- #endif
- #ifndef __APPLEEVENTS__
- #include <AppleEvents.h>
- #endif
- #ifndef __AEOBJECTS__
- #include <AEObjects.h>
- #endif
- #ifndef __OSA__
- #include <OSA.h>
- #endif
- #ifndef __APPLESCRIPT__
- #include <AppleScript.h>
- #endif
- #ifndef __ASDEBUGGING__
- #include <ASDebugging.h>
- #endif
-
- #ifndef __AEOMTOKENS__
- #include "ObjModelTokens.h"
- #endif
- #ifndef __AEOMEVENTS__
- #include "ObjModelEvents.h"
- #endif
-
- #ifndef __SCRIPTOBJECTS__
- #include "ScriptableObjects.h"
- #endif
-
- #ifndef __SCRIPTUTILS__
- #include "ScriptUtils.h"
- #endif
-
-
- extern "C" {
- #include "AEBuild.h"
- #include "AEBuildGlobals.h"
- #include "AEPrint.h"
- }
- #include "AEBuildErrMsgs.h"
-
-
- #include "DebugTrace.h"
-
-
-
- #pragma segment AEventHandlers
-
-
- void DumpAppleEvent(char *msg, AEDesc *desc)
- {
- if (msg)
- printf("%s", msg);
-
- if (desc)
- {
- char buf[3000];
- if (AEPrint(desc, buf, 3000) == 0)
- {
- printf(buf);
- printf("\n");
- }
- }
- }
-
-
-
- OSErr GetCurrentAppFileSpec(short *vRefNum, long *dirID, StringPtr fileName)
- {
- OSErr err = 0;
- ProcessInfoRec pInfo;
- ProcessSerialNumber pNum;
- FSSpec fSpec;
-
- err = GetCurrentProcess(&pNum);
- if (!err)
- {
- pInfo.processInfoLength = sizeof(pInfo);
- pInfo.processName = NULL;
- pInfo.processAppSpec = &fSpec;
- err = GetProcessInformation(&pNum, &pInfo);
- }
- if (!err)
- {
- *vRefNum = fSpec.vRefNum;
- *dirID = fSpec.parID;
- PLstrcpy((StringPtr)fileName, fSpec.name);
- }
-
- return err;
- }
-
-
- void GetCurrAppName(CStr255& appName)
- {
- short vRefNum;
- long dirID;
- Str255 fileName;
- OSErr err = GetCurrentAppFileSpec(&vRefNum, &dirID, (StringPtr)fileName);
-
- if (!err)
- appName = fileName;
- }
-
-
-
-
- #define optionKeyCode 0x3A
- #define shiftKeyCode 0x38
- #define cmdKeyCode 0x37
- #define ctrlKeyCode 0x3B
-
- static Boolean isKeyDown(long keyCode)
- {
- KeyMap keys;
- Byte *keysBytes;
-
- GetKeys(keys);
-
- keysBytes = (Byte *)keys;
-
- return((keysBytes[keyCode/8] & (1 << keyCode % 8)) != 0);
- }
-
-
- static pascal Boolean StdIdleFunction (EventRecord *theEventRecord,
- long *sleepTime,
- RgnHandle *mouseRgn)
- {
- return false;
- }
-
-
- static pascal OSErr GetAETEAEvtHandler (AppleEvent *theEvent,
- AppleEvent *theReply,
- long theRefCon)
- {
- OSAError err = errAEEventNotHandled;
- AEDesc dataDesc, resultDesc;
- short languageCode;
-
- InitAEDescs(&dataDesc, &resultDesc, kEndOfList);
-
- err = AEGetParamDesc(theEvent, keyDirectObject, typeAEList, &dataDesc);
- if (!err)
- err = GetIntegerFromDescriptor(&dataDesc, &languageCode);
- if (!err)
- err = AECreateList(NULL, 0, false, &resultDesc);
-
- if (!err && gSimpliFace)
- err = gSimpliFace->CollectAETEs(languageCode, resultDesc);
-
- if (!err)
- err = AEPutParamDesc(theReply, keyDirectObject, &resultDesc);
-
- DisposeAEDescs(&dataDesc, &resultDesc, kEndOfList);
-
- return (OSErr)err;
- }
-
-
- static pascal OSErr ExecuteEventInContext (AppleEvent *theEvent,
- AppleEvent *theReply,
- long theRefCon,
- OSAID theScriptID,
- AEHandlerProcPtr theContextHandler,
- long dispatchRefCon)
- {
- OSAError err = errAEEventNotHandled;
- AEHandlerProcPtr oldResumeProc;
- long oldRefcon;
-
- OSAGetResumeDispatchProc(gScriptingComponent, &oldResumeProc, &oldRefcon);
-
- // pass to script for handling
- if (theScriptID != kOSANullScript)
- {
- err = OSASetResumeDispatchProc(gScriptingComponent, theContextHandler, dispatchRefCon);
- if (!err)
- {
- err = OSADoEvent(gScriptingComponent, theEvent, theScriptID,
- kOSAModeAlwaysInteract, theReply);
- }
-
- OSASetResumeDispatchProc(gScriptingComponent, oldResumeProc, oldRefcon);
- }
-
- return (OSErr)err;
- }
-
-
- static pascal OSErr StdAEvtPreHandler (AppleEvent *theEvent,
- AppleEvent *theReply,
- long theRefCon)
- {
- OSAError err = errAEEventNotHandled;
- AEEventClass theEvtClass;
- AEEventID theEvtID;
- DescType theType;
- Size theSize;
- AEDesc directParam, theTokenDesc;
- OSAID theScriptID = kOSANullScript;
- objModelTokenPtr theToken = nil;
- Boolean madeAppToken = false;
- DescType theTokenClass;
-
- theTokenDesc.descriptorType = typeNull;
- theTokenDesc.dataHandle = nil;
-
- // Extract the class and ID of the event from the AppleEvent
- AEGetAttributePtr(theEvent, keyEventClassAttr, typeType, &theType, (Ptr) &theEvtClass, sizeof(theEvtClass), &theSize);
- AEGetAttributePtr(theEvent, keyEventIDAttr, typeType, &theType, (Ptr) &theEvtID, sizeof(theEvtID), &theSize);
-
- DumpAppleEvent("••StdAEvtPreHandler: ", theEvent);
-
- if (isKeyDown(ctrlKeyCode) && isKeyDown(cmdKeyCode))
- // do nothing if user is by-passing scripted event-handling
- printf("User by-passed script\n");
- else if ((theEvtClass == kCoreEventClass && theEvtID != kAEOpenApplication)
- || (theEvtClass == kAECoreSuite && theEvtID != kAESetData && theEvtID != kAEGetData)
- || (theEvtClass == kSignature && theEvtID == kAESystemEvent)
- || (theEvtClass == kASAppleScriptSuite && theEvtID == kASSubroutineEvent))
- { // above test is to make sure we handle all the scriptable events
- // and that we skip the ones we don't want to be scriptable
- err = AEGetParamDesc(theEvent, keyDirectObject, typeWildCard, &directParam);
- if (!err)
- {
- err = AEResolve(&directParam, kAEIDoMinimum, &theTokenDesc);
- AEDisposeDesc(&directParam);
-
- if (!err)
- theToken = ObjModelTokenFromDesc(&theTokenDesc);
- }
- if (err || !theToken)
- {
- err = MakeAppToken((TObjModelToken**)&theToken);
- madeAppToken = (!err);
- }
-
- if (theToken)
- theScriptID = gScriptAdministrator->GetAttachedScript(theToken->GetTokenObj());
-
- // pass to script for handling
- if (theScriptID != kOSANullScript)
- err = ExecuteEventInContext(theEvent, theReply, theRefCon, theScriptID,
- kOSAUseStandardDispatch, kOSADontUsePhac);
- else
- err = errAEEventNotHandled;
-
- if (theToken)
- gScriptAdministrator->ReleaseAttachedScript(theToken->GetTokenObj());
-
- // this will dispose of the token as well as the descriptor, so
- AEDisposeToken(&theTokenDesc); // DO NOT CALL until done with 'theToken'
- if (madeAppToken)
- delete theToken; // this will only be executed if token was made in this function
- }
-
- if (err == errAEEventNotHandled)
- printf("StdAEvtPreHandler: Apple Event not handled in pre-handler\n");
- else if (err)
- printf("StdAEvtPreHandler: err = %d\n", err);
-
- return (OSErr)err;
- }
-
-
- static pascal OSErr RqdAEvtHandler (AppleEvent *theEvent,
- AppleEvent *theReply,
- long theRefCon)
- {
- OSErr err = errAEEventNotHandled;
- AEEventClass theEvtClass;
- AEEventID theEvtID;
- DescType theType;
- Size theSize;
-
- // Extract the class and ID of the event from the AppleEvent
- AEGetAttributePtr(theEvent, keyEventClassAttr, typeType, &theType, (Ptr) &theEvtClass, sizeof(theEvtClass), &theSize);
- AEGetAttributePtr(theEvent, keyEventIDAttr, typeType, &theType, (Ptr) &theEvtID, sizeof(theEvtID), &theSize);
-
- printf("•RqdAEvtHandler: class=%.4s, id=%.4s\n", &theEvtClass, &theEvtID);
-
- if (theEvtClass == kCoreEventClass)
- {
- if (theEvtID == kAEOpenApplication)
- {
- if (isKeyDown(ctrlKeyCode) && isKeyDown(cmdKeyCode))
- ;
- else
- {
- if (gScriptAdministrator)
- gScriptAdministrator->RunStartupScript();
- err = 0;
- }
- }
- else if (theEvtID == kAEQuitApplication)
- {
- err = 0;
- gSimpliFace->Terminate(); // quit after next time around event loop
- }
- }
-
- printf("RqdAEvtHandler: err = %d\n", err);
-
- return err;
- }
-
-
- static pascal OSErr StdAEvtHandler (AppleEvent *theEvent,
- AppleEvent *theReply,
- long theRefCon)
- {
- OSAError err = errAEEventNotHandled;
- AEEventClass theEvtClass;
- AEEventID theEvtID;
- DescType theType;
- Size theSize;
- AEDesc directParam, theTokenDesc;
- TObjModelToken *theToken = nil;
- Boolean madeAppToken = false;
-
- theTokenDesc.descriptorType = typeNull;
- theTokenDesc.dataHandle = nil;
-
- // Extract the class and ID of the event from the AppleEvent
- AEGetAttributePtr(theEvent, keyEventClassAttr, typeType, &theType, (Ptr) &theEvtClass, sizeof(theEvtClass), &theSize);
- AEGetAttributePtr(theEvent, keyEventIDAttr, typeType, &theType, (Ptr) &theEvtID, sizeof(theEvtID), &theSize);
-
- printf("•StdAEvtHandler: class=%.4s, id=%.4s\n", &theEvtClass, &theEvtID);
-
- if (theEvtClass == kAECoreSuite && theEvtID == kAECreateElement)
- {
- err = MakeAppToken(&theToken);
- madeAppToken = (!err);
- }
- else
- {
- err = AEGetParamDesc(theEvent, keyDirectObject, typeWildCard, &directParam);
- if (!err)
- {
- if (theEvtClass == kCoreEventClass
- && theEvtID == kAEOpenDocuments
- && (directParam.descriptorType == typeAEList))
- {
- // open documents here
- err = errAEEventNotHandled;
- }
- else
- {
- err = AEResolve(&directParam, kAEIDoMinimum, &theTokenDesc);
-
- if (!err)
- theToken = (TObjModelToken*)ObjModelTokenFromDesc(&theTokenDesc);
- }
- AEDisposeDesc(&directParam);
- }
-
- if (err)
- {
- err = MakeAppToken(&theToken);
- madeAppToken = (!err);
- }
- }
- if (theToken)
- {
- err = theToken->DispatchAppleEvent(theEvent, theReply, theEvtClass, theEvtID);
- }
- // this will dispose of the token as well as the descriptor, so
- AEDisposeToken(&theTokenDesc); // DO NOT CALL until done with 'theToken'
- if (madeAppToken)
- delete theToken; // this will only be executed if token was made in this function
-
- printf("StdAEvtHandler: err = %ld\n", err);
-
- return (OSErr)err;
- }
-
-
- static pascal OSErr DoScriptAEvtHandler (AppleEvent *theEvent,
- AppleEvent *theReply,
- long theRefCon)
- {
- OSAError err = errAEEventNotHandled;
- AEEventClass theEvtClass;
- AEEventID theEvtID;
- DescType theType;
- Size theSize;
-
- // Extract the class and ID of the event from the AppleEvent
- AEGetAttributePtr(theEvent, keyEventClassAttr, typeType, &theType, (Ptr) &theEvtClass, sizeof(theEvtClass), &theSize);
- AEGetAttributePtr(theEvent, keyEventIDAttr, typeType, &theType, (Ptr) &theEvtID, sizeof(theEvtID), &theSize);
-
- printf("•DoScriptAEvtHandler: class=%.4s, id=%.4s\n", &theEvtClass, &theEvtID);
-
- if (gScriptAdministrator)
- {
- AEDesc sourceDesc, resultDesc;
-
- InitAEDescs(&sourceDesc, &resultDesc, kEndOfList);
-
- err = AEGetParamDesc(theEvent, keyDirectObject, typeChar, &sourceDesc);
- if (!err)
- err = gScriptAdministrator->DoScript(&sourceDesc, &resultDesc);
- if (!err)
- err = AEPutParamDesc(theReply, keyDirectObject, &resultDesc);
-
- DisposeAEDescs(&sourceDesc, &resultDesc, kEndOfList);
- }
-
- printf("DoScriptAEvtHandler: err = %ld\n", err);
-
- return err;
- }
-
-
-
- // installation
-
- pascal OSErr InstallEventHandlers(void)
- {
- OSErr err = 0;
-
- if (!err) err = AEInstallSpecialHandler(keyPreDispatch, (EventHandlerProcPtr)&StdAEvtPreHandler, false);
-
- if (!err) err = AEInstallEventHandler(kASAppleScriptSuite, kGetAETE, (EventHandlerProcPtr)&GetAETEAEvtHandler, (long)kGetAEUT, false);
-
- if (!err) err = AEInstallEventHandler(kCoreEventClass, typeWildCard, (EventHandlerProcPtr)&RqdAEvtHandler, 0, false);
-
- if (!err) err = AEInstallEventHandler(kAECoreSuite, typeWildCard, (EventHandlerProcPtr)&StdAEvtHandler, 0, false);
- if (!err) err = AEInstallEventHandler(kCoreEventClass, kAEOpen, (EventHandlerProcPtr)&StdAEvtHandler, (long)kAEOpen, false);
-
- if (!err) err = AEInstallEventHandler(kAEMiscStandards, kAEDoScript, (EventHandlerProcPtr)&DoScriptAEvtHandler, (long)kAEDoScript, false);
-
- return err;
- }
-
- pascal OSErr DeInstallEventHandlers(void)
- {
- OSErr err = 0;
-
- return err;
- }
-
-
-